//
//  RxDataSourcesTest3ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

class RxDataSourcesTest3ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "29.3.多分区的TableView - 使用自带的Section"
        view.backgroundColor = .white
        
        test()
    }
    
    func test() {
        //创建一个重用的单元格
       self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
       self.view.addSubview(self.tableView!)
        
       //初始化数据
       let items = Observable.just([
           SectionModel(model: "基本控件", items: [
               "UILable的用法",
               "UIText的用法",
               "UIButton的用法"
               ]),
           SectionModel(model: "高级控件", items: [
               "UITableView的用法",
               "UICollectionViews的用法"
               ])
           ])
        
       //创建数据源
       let dataSource = RxTableViewSectionedReloadDataSource
           <SectionModel<String, String>>(configureCell: {
           (dataSource, tv, indexPath, element) in
           let cell = tv.dequeueReusableCell(withIdentifier: "Cell")!
           cell.textLabel?.text = "\(indexPath.row)：\(element)"
           return cell
       })
        
       //设置分区头标题
       dataSource.titleForHeaderInSection = { ds, index in
           return ds.sectionModels[index].model
       }
        
       //设置分区尾标题
       //dataSource.titleForFooterInSection = { ds, index in
       //    return "footer"
       //}
        
       //绑定单元格数据
       items
           .bind(to: tableView.rx.items(dataSource: dataSource))
           .disposed(by: disposeBag)
}

}
